package com.xinyue.fishinghunter.fish.handlers;

import org.springframework.beans.factory.annotation.Autowired;

import com.xinyue.fishinghunter.dao.entity.FishingHunterPlayer;
import com.xinyue.fishinghunter.fish.messages.UpdatePlayerMessageRequest;
import com.xinyue.fishinghunter.fish.messages.UpdatePlayerMessageResponse;
import com.xinyue.fishinghunter.fish.messages.body.UpdatePlayerRequestBody;
import com.xinyue.fishinghunter.fish.service.PlayerService;
import com.xinyue.fishinghunter.fish.service.rank.EnumRankType;
import com.xinyue.fishinghunter.fish.service.rank.RankService;
import com.xinyue.game.logic.frame.GameServerContext;
import com.xinyue.network.message.handler.AbstractGameMessageHandler;
import com.xinyue.network.message.handler.GameMessageHandler;
import com.xinyue.network.message.handler.GameRequestMapping;

@GameMessageHandler
public class PlayerHandler extends AbstractGameMessageHandler {
	@Autowired
	private PlayerService playerService;
	@Autowired
	private RankService rankService;

	@GameRequestMapping(UpdatePlayerMessageRequest.class)
	public void updatePlayer(UpdatePlayerMessageRequest request, GameServerContext ctx) {
		long playerId = request.getHeader().getPlayerId();
		FishingHunterPlayer player = playerService.getPlayer(playerId).get();
		UpdatePlayerRequestBody body = request.getBodyObj();
		player.setBullet(body.getBullet());
		player.setGold(body.getGold());
		player.setScore(body.getScore());
		playerService.updatePlayer(player);
		rankService.updateRank(playerId, player.getScore(), EnumRankType.ScoreRank);
		UpdatePlayerMessageResponse response = request.createCouple();
		int myRank = rankService.getMyRank(playerId, EnumRankType.ScoreRank);
		response.getBodyObj().setMyRank(myRank);
		ctx.sendToGateway(response);
	}
}
